package org.yagnus.stats.samplers.discrete;

import java.util.ArrayList;
import java.util.List;

import org.yagnus.stats.sampler.Sampler;

/**
 * 
 * @author hc.busy
 * 
 * @param <BASETYPE>
 */
public abstract class ListSampler<BASETYPE> extends Sampler {

	protected abstract void _init(List<BASETYPE> t, List<Double> weights);

	/**
	 * This function constructs a sampler from specified array and corresponding
	 * weights
	 * 
	 * @param t
	 *            the sampmle spae
	 * @param weights
	 *            the weights corresponding to each item
	 * @return
	 */

	public ListSampler(List<BASETYPE> t, List<Double> weights) {
		super();
		_init(t, weights);
	}

	public ListSampler(List<BASETYPE> t) {
		super();
		List<Double> w2 = new ArrayList<Double>(t.size());
		for (int i = 0; i < w2.size(); ++i) {
			w2.add(1d);
		}
		_init(t, w2);
	}

	/**
	 * 
	 * this function modifies the sampling problem to add to the sample space
	 * 
	 * @param t
	 *            the new member of the population
	 * @param w
	 *            its weight.
	 */
	public abstract void addSample(BASETYPE t, double w);

	public void addSamples(BASETYPE[] t, double[] w) {
		if (t.length != w.length) {
			throw new IllegalArgumentException(
					"Sample and weights do not have same lengths");
		}
		for (int i = 0; i < t.length; ++i) {
			addSample(t[i], w[i]);
		}
	}

	/**
	 * Removes all instances of the object t from sample space
	 * 
	 * @param t
	 *            the the sample to remove
	 */
	public abstract void removeSample(BASETYPE t);

	public void removeSamples(BASETYPE[] t) {
		for (int i = 0; i < t.length; ++i) {
			removeSample(t[i]);
		}
	}

}
